﻿Imports System.Web
Imports System.Web.Security
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.IO.FileInfo
Imports System.IO.FileStream
Imports System.IO.BinaryReader
Imports System.Data.SqlClient


Imports YAF.Core
Imports YAF.Core.Services
Imports YAF.Types
Imports YAF.Types.Constants
Imports YAF.Types.EventProxies
Imports YAF.Types.Interfaces

Imports System.IO
Imports System.Data
Public Class GetDL
    Inherits System.Web.UI.Page
    Dim isreg As Boolean = False
    Dim connStr As String = ConfigurationManager.ConnectionStrings("yafnet").ConnectionString
    Public Sub Recorddl(Fileid As String)

        Dim dcon As New SqlClient.SqlConnection
        Dim dquery As New SqlClient.SqlCommand
        dcon.ConnectionString = connStr
        dquery.Connection = dcon
        'Select Query here
        dquery.CommandText = "INSERT INTO Downloads   (FileID, Userid) VALUES        (" & Fileid & ", " & YafContext.Current.PageUserID & ")"
        dcon.Open()
        dquery.ExecuteNonQuery()

        dcon.Close()

    End Sub

    Private Sub downloadfile(Fileid As String, filepath As String)
        Dim fs As FileStream
        Dim strPath As String = Server.MapPath("/Downloads/")
        Dim strFileName As String = filepath
        If isreg = True Then
            If File.Exists(strPath & strFileName) = True Then


                fs = File.Open(strPath & strFileName, FileMode.Open)
                Dim bytBytes(fs.Length) As Byte
                fs.Read(bytBytes, 0, fs.Length)
                fs.Close()
                Recorddl(Fileid)
                Response.AddHeader("Content-disposition", _
                           "attachment; filename=" & strFileName)
                Response.ContentType = "application/octet-stream"
                Response.BinaryWrite(bytBytes)
                Response.End()

            Else

                Label1.Text = ("Sorry the file is currently unavaliable or has been removed!")

            End If
        Else
            Label1.Text = ("Sorry. You don't have access" & _
              " to this file. Please return to the login page and try again.")
        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        ' admin or moderator, set edit control to moderator mode...

        If YafContext.Current.CurrentUserData.IsGuest Then


            Response.Redirect("http://xwinn.com/forums.aspx?g=login&ReturnUrl=%2fdownload.aspx")


        Else
            'Label1.Text = YafContext.Current.User.UserName & YafContext.Current.User.Email & YafContext.Current.PageUserID & YafContext.Current.CurrentUserData.RankName
            isreg = True
            Label1.Text = Request.Params("FileID") & Request.Params("FilePath")
            downloadfile(Request.Params("FileID"), Request.Params("FilePath"))
        End If
    End Sub
End Class
